package com.example.anliku.service.serviceImpl;

import com.example.anliku.Utils.RedisUtil;
import com.example.anliku.entity.*;
import com.example.anliku.mapper.ClazzMapper;
import com.example.anliku.mapper.ExampleCommentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.example.anliku.mapper.CommentMapper;
import com.example.anliku.service.CommentService;

import java.lang.System;
import java.util.*;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private ExampleCommentMapper exampleCommentMapper;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Message deleteByPrimaryKey(Integer commentId, Integer exampleId) {
        redisUtil.decr("comment" + exampleId, 1);
        exampleCommentMapper.deleteByExampleCommentExampleIdAndExampleCommentCommentId(exampleId,commentId);
        return commentMapper.deleteByPrimaryKey(commentId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Comment record) {
        return commentMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Comment record) {
        if (commentMapper.insertSelective(record) > 0) {
            ExampleComment exampleComment = new ExampleComment();
            exampleComment.setExampleCommentCommentId(record.getCommentId());
            exampleComment.setExampleCommentExampleId(Integer.valueOf(record.getCommentCaseId()));
            exampleCommentMapper.insertSelective(exampleComment);
            return Message.success(null).add(record.getCommentId());
        } else {
            return Message.fail(null);
        }
    }

    @Override
    public Message selectByPrimaryKey(Integer commentId) {
        return Message.success(null).add(commentMapper.selectByPrimaryKey(commentId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Comment record) {
        return commentMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Comment record) {
        return commentMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll() {
        return Message.success(null).add(commentMapper.selectAll());
    }

    @Override
    public Message updateByCommentIdAndCommentState(Integer commentId, Integer commentState, String addedCommentCheckId) {
        String id = commentMapper.selectByPrimaryKey(commentId).getCommentCaseId();
        if (commentState == 1) {
            boolean rs1 = redisUtil.hasKey("comment" + id);
            if (!rs1) {
                redisUtil.set("comment" + id, 1);
            } else {
                redisUtil.incr("comment" + id, 1);
            }
        }

        return commentMapper.updateCommentStateAndCommentPassTimeAndIncCommentCheckIdByCommentId(commentState, new Date(), addedCommentCheckId, commentId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public List<Map> returnSum() {
        List<Comment> commentList = commentMapper.selectAll();
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < commentList.size(); i++) {
            if (commentList.get(i).getCommentState() == 1) {
                if (map.get(commentList.get(i).getCommentCaseId()) != null) {
                    map.put(commentList.get(i).getCommentCaseId(), map.get(commentList.get(i).getCommentCaseId()) + 1);
                } else {
                    map.put(commentList.get(i).getCommentCaseId(), 1);
                }
            }

        }
        //得到map中所有的键
        Set<String> keyset = map.keySet();
        //创建set集合的迭代器
        Iterator<String> it = keyset.iterator();
        List<Map> mapList = new LinkedList<>();
        while (it.hasNext()) {
            HashMap maps = new HashMap();
            String key = it.next();
            Integer value = map.get(key);
            maps.put("commentCaseId", key);
            maps.put("commentSum", value);
            mapList.add(maps);
        }
        return mapList;
    }


}


